<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2017/1/6
 * Time: 11:03
 */

require "../vendor/autoload.php";
use Elasticsearch\Client;
$client = new Client();

$indexParams['index']  = 'index_zing';    //index
$arr = $client->indices()->create( $indexParams );
//var_dump($arr);


$indexParams['index']  = 'my_index';
// Index Settings
$indexParams['body']['settings']['number_of_shards']   = 3;
$indexParams['body']['settings']['number_of_replicas'] = 2;
// Example Index Mapping
$myTypeMapping = array(
	'_source' => array(
		'enabled' => true
	),
	'properties' => array(
		'first_name' => array(
			'type' => 'string',
			'analyzer' => 'standard'
		),
		'age' => array(
			'type' => 'integer'
		)
	)
);
$indexParams['body']['mappings']['my_type'] = $myTypeMapping;

// Create the index
$client->indices()->create($indexParams);

//$result = $client->get( [ 'index' => 'my_index' ] );
$result = $client->indices()->get(['index'=>'my_index']);
print_r($result);

//Delete an index
//$deleteParams['index'] = 'my_index';
//$client->indices()->delete($deleteParams);


$paramss = [
	'index' => 'reuters',
	'body' => [
//		The top level settings contains config about the index (# of shards, etc) as well as analyzers
		'settings' => [
			'number_of_shards' => 1,
			'number_of_replicas' => 0,
//			analysis is nested inside of settings, and contains tokenizers, filters, char filters and analyzers
			'analysis' => [
				'filter' => [
					'shingle' => [
						'type' => 'shingle'
					]
				],
				'char_filter' => [
					'pre_negs' => [
						'type' => 'pattern_replace',
						'pattern' => '(\\w+)\\s+((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\b',
						'replacement' => '~$1 $2'
					],
					'post_negs' => [
						'type' => 'pattern_replace',
						'pattern' => '\\b((?i:never|no|nothing|nowhere|noone|none|not|havent|hasnt|hadnt|cant|couldnt|shouldnt|wont|wouldnt|dont|doesnt|didnt|isnt|arent|aint))\\s+(\\w+)',
						'replacement' => '$1 ~$2'
					]
				],
				'analyzer' => [
					'reuters' => [
						'type' => 'custom',
						'tokenizer' => 'standard',
						'filter' => ['lowercase', 'stop', 'kstem']
					]
				]
			]
		],
//		mappings is another top level element nested inside of body, and contains the mappings for various types
		'mappings' => [

//			The _default_ type is a dynamic template that is applied to all fields that don’t have an explicit mapping
			'_default_' => [
				'properties' => [
					'title' => [
						'type' => 'string',
						'analyzer' => 'reuters',
						'term_vector' => 'yes',
						'copy_to' => 'combined'
					],
					'body' => [
						'type' => 'string',
						'analyzer' => 'reuters',
						'term_vector' => 'yes',
						'copy_to' => 'combined'
					],
					'combined' => [
						'type' => 'string',
						'analyzer' => 'reuters',
						'term_vector' => 'yes'
					],
					'topics' => [
						'type' => 'string',
						'index' => 'not_analyzed'
					],
					'places' => [
						'type' => 'string',
						'index' => 'not_analyzed'
					]
				]
			],

//			The my_type type is an example of a user-defined type that holds a single field, my_field
			'my_type' => [
				'properties' => [
					'my_field' => [
						'type' => 'string'
					]
				]
			]
		]
	]
];
$client->indices()->create($paramss);
$result = $client->indices()->get(['index' => 'reuters']);
print_r($result);

// Get mappings for all indexes and types
$result = $client->indices()->getMapping();
print_r($result);

echo "-------------------------------------------".PHP_EOL;
// Get mappings for all types in 'my_index'
$params['index'] = 'index_zing';
$ret = $client->indices()->getMapping($params);
print_r($ret);

echo "-------------------------------------------".PHP_EOL;
// Get mapping 'my_type' in 'my_index'
$params['index'] = 'megacorp';
$params['type']  = 'employee';
$ret = $client->indices()->getMapping($params);
print_r($ret);


echo "-------------------------------------------".PHP_EOL;
// Get mappings for two indexes
$params['index'] = array('megacorp', 'index_zing');
$ret = $client->indices()->getMapping($params);
print_r($ret);


$client->indices()->delete(['index' => 'reuters']);

$deleteParams['index'] = 'my_index';
$client->indices()->delete($deleteParams);